#include "logging.h"
#include "./ui_logging.h"
#include "tcpclient.h"
#include "chat.h"
#include <QMessageBox>
#include <QString>
Logging::Logging(QWidget *parent) : QWidget(parent), ui(new Ui::Logging) {
    ui->setupUi(this);
    t = new TcpClient(this);
    msgBox = new QMessageBox(this);
    Init();
}

Logging::~Logging() { delete ui; }

void Logging::Init() {
    this->setWindowTitle("登录");
    connect(t, &TcpClient::CallLogging, this,
            [this](const QJsonObject &msg) { CmdHandler(msg); });
}

void Logging::on_pushBtn_logging_clicked() {
    if (!t->m_isConnected) {
        qDebug() << "未连接" << endl;
        if (t->ConnectToServer() == -1) {
            qDebug() << "连接失败" << endl;
            return;
        }
    }
    if (!ui->line_number->text().size()) {
        msgBox->setText("账号不能为空");
        msgBox->showNormal();
        return;
    }
    if (!ui->line_password->text().size()) {
        msgBox->setText("密码不能为空");
        msgBox->showNormal();
        return;
    }
    json msg;
    msg.insert("msgid", static_cast<int>(EnMsgType::LOGIN_MSG));
    msg.insert("id", ui->line_number->text().toInt());
    msg.insert("password", ui->line_password->text());
    t->SendMsg(msg);
}

void Logging::CmdHandler(json msg) {
    if (msg.isEmpty())
        return;
    int cmd = msg.value("msgid").toInt();
    switch (cmd) {
    case static_cast<int>(EnMsgType::LOGIN_MSG_ACK): {
        if (msg.value("errno").toInt() == 0) {
            qDebug() << "登录成功";
            SelfInfo info;
            info.name = msg.value("name").toString();
            info.account = msg.value("id").toInt();
            info.password = ui->line_password->text();
            static Chat *chat = new Chat(info, t);
            chat->show();
            this->close();
        } else {
            qDebug() << msg.value("errmsg").toString() << endl;
            QString errmsg = "登录失败";
            if (!msg.value("errmsg").isNull()) {
                errmsg = msg.value("errmsg").toString();
            }
            msgBox->setText(errmsg);
            msgBox->showNormal();
        }

        break;
    }

    case static_cast<int>(EnMsgType::REG_MSG_ACK): {
        if (msg.value("errno").toInt() == 0) {
            qDebug() << "注册成功";
            msgBox->setText(QString("注册成功： 请牢记您的id: %1").arg(msg.value("id").toInt()));
            msgBox->showNormal();
        } else {
            qDebug() << msg.value("errmsg").toString() << endl;
            QString errmsg = "注册失败";
            if (!msg.value("errmsg").isNull()) {
                errmsg = msg.value("errmsg").toString();
            }
            msgBox->setText(errmsg);
            msgBox->showNormal();
        }

    }
    default:
        break;
    }
}

void Logging::on_pushBtn_register_clicked()
{
    if (!t->m_isConnected) {
        qDebug() << "未连接" << endl;
        if (t->ConnectToServer() == -1) {
            qDebug() << "连接失败" << endl;
            return;
        }
    }
    if (!ui->line_number->text().size()) {
        msgBox->setText("用户名不能为空");
        msgBox->showNormal();
        return;
    }
    if (!ui->line_password->text().size()) {
        msgBox->setText("密码不能为空");
        msgBox->showNormal();
        return;
    }
    json msg;
    msg.insert("msgid", static_cast<int>(EnMsgType::REG_MSG));
    msg.insert("name", ui->line_number->text());
    msg.insert("password", ui->line_password->text());
    t->SendMsg(msg);
}

